package com.xh.jf_blog.service.impl;

import com.xh.jf_blog.bean.Echarts;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.mapper.EchartsMapper;
import com.xh.jf_blog.service.EchartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author PangJunjie
 * @Date 2024/3/12/012
 */
@Service
@Transactional
public class EchartsServiceImpl implements EchartsService {
    @Autowired
    private EchartsMapper echartsMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

    @Override
    public R getBarList() {
        List<Echarts> articleList = echartsMapper.getArticleBar();
        List<Echarts> commentList = echartsMapper.getCommentBar();

        List<Echarts> al = new ArrayList<>();
        List<Echarts> cl = new ArrayList<>();

        for (int i = 5; i >= 0; i--) {
            Date date = new Date();
            date.setMonth(date.getMonth() - i);
            String text = sdf.format(date);
            boolean isArticleGet = false;
            Echarts articleEcharts = new Echarts();
            articleEcharts.setText(text);
            for (int j = 0; j < articleList.size(); j++) {
                if (text.equals(articleList.get(j).getText())) {
                    isArticleGet = true;
                    articleEcharts.setCount(articleList.get(j).getCount());
                    break;
                }
            }
            if (!isArticleGet) {
                articleEcharts.setCount(0);
            }
            al.add(articleEcharts);

            boolean isCommentGet = false;
            Echarts commentEcharts = new Echarts();
            commentEcharts.setText(text);
            for (int j = 0; j < commentList.size(); j++) {
                if (text.equals(commentList.get(j).getText())) {
                    isCommentGet = true;
                    commentEcharts.setCount(commentList.get(j).getCount());
                    break;
                }
            }
            if (!isCommentGet) {
                commentEcharts.setCount(0);
            }
            cl.add(commentEcharts);
        }
        Map<String, List<Echarts>> map = new HashMap<>();
        map.put("articleList", al);
        map.put("commentList", cl);

        return R.ok(map);
    }

    @Override
    public R getLineList() {
        List<Echarts> userList = echartsMapper.getUserLine();
        List<Echarts> ul = new ArrayList<>();
        for (int i = 5; i >= 0; i--) {
            Date date = new Date();
            date.setMonth(date.getMonth() - i);
            String text = sdf.format(date);
            boolean isGet = false;
            Echarts echarts = new Echarts();
            echarts.setText(text);
            for (int j = 0; j < userList.size(); j++) {
                if (text.equals(userList.get(j).getText())) {
                    isGet = true;
                    echarts.setCount(userList.get(j).getCount());
                    break;
                }
            }
            if (!isGet) {
                echarts.setCount(0);
            }
            ul.add(echarts);
        }
        return R.ok(ul);
    }

    @Override
    public R getPieList() {
        return R.ok(echartsMapper.getCategoryPie());
    }

    @Override
    public R getRadarList() {
        Echarts userinfo1 = echartsMapper.getUserinfoList1();
        Echarts userinfo2 = echartsMapper.getUserinfoList2();
        Echarts article1 = echartsMapper.getArticleList1();
        Echarts article2 = echartsMapper.getArticleList2();
        Echarts comment1 = echartsMapper.getCommentList1();
        Echarts comment2 = echartsMapper.getCommentList2();
        Echarts anthology1 = echartsMapper.getAnthologyList1();
        Echarts anthology2 = echartsMapper.getAnthologyList2();
        Echarts category1 = echartsMapper.getCategoryList1();
        Echarts category2 = echartsMapper.getCategoryList2();
        Echarts tag1 = echartsMapper.getTagList1();
        Echarts tag2 = echartsMapper.getTagList2();

        List<String> legendTitle = new ArrayList<>();
        legendTitle.add(userinfo1.getText());
        legendTitle.add(userinfo2.getText());

        List<Map<String, Object>> indicator = new ArrayList<>();
        Map<String, Object> ind = new HashMap<>();
        ind.put("name", "会员数");
        ind.put("max", Math.max(userinfo1.getCount(), userinfo2.getCount()) + 3);
        indicator.add(ind);
        ind = new HashMap<>();
        ind.put("name", "文章数");
        ind.put("max", Math.max(article1.getCount(), article2.getCount()) + 3);
        indicator.add(ind);
        ind = new HashMap<>();
        ind.put("name", "评论数");
        ind.put("max", Math.max(comment1.getCount(), comment2.getCount()) + 3);
        indicator.add(ind);
        ind = new HashMap<>();
        ind.put("name", "文集数");
        ind.put("max", Math.max(anthology1.getCount(), anthology2.getCount()) + 3);
        indicator.add(ind);
        ind = new HashMap<>();
        ind.put("name", "分类数");
        ind.put("max", Math.max(category1.getCount(), category2.getCount()) + 3);
        indicator.add(ind);
        ind = new HashMap<>();
        ind.put("name", "标签数");
        ind.put("max", Math.max(tag1.getCount(), tag2.getCount()) + 3);
        indicator.add(ind);


        List<Map<String, Object>> seriesData = new ArrayList<>();
        Map<String, Object> ser = new HashMap<>();
        ser.put("name", userinfo1.getText());
        ser.put("value", new int[]{userinfo1.getCount(), article1.getCount(), comment1.getCount(), anthology1.getCount(), category1.getCount(), tag1.getCount()});
        seriesData.add(ser);
        ser = new HashMap<>();
        ser.put("name", userinfo2.getText());
        ser.put("value", new int[]{userinfo2.getCount(), article2.getCount(), comment2.getCount(), anthology2.getCount(), category2.getCount(), tag2.getCount()});
        seriesData.add(ser);

        Map<String, Object> map = new HashMap<>();
        map.put("legendTitle", legendTitle);
        map.put("indicator", indicator);
        map.put("seriesData", seriesData);
        return R.ok(map);
    }

}
